package com.zwz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zwz.bean.ShiroUser;
import com.zwz.bean.SysUsersBean;
import com.zwz.model.SysUsers;
import com.zwz.service.baseConfig.SysUserService;
import com.zwz.service.staticConfig.RoleAuthService;

/**
 * 用户管理
 * @author zhangwanzhong
 * 2015年12月31日 下午3:23:13
 */
@Controller
@RequestMapping("/user")
public class SysUserController extends BaseController {
	@Autowired
	private SysUserService sysUserService;
	
	@RequestMapping(value="getView")
	public String getView(HttpServletRequest request){
		request.setAttribute("roleAuth", getRoleAuth());
		return "user";
	}
	
	@RequestMapping(value="/queryByList")
	@ResponseBody
	public Object queryByList(SysUsersBean model)throws Exception{
		List<SysUsers> dataList=sysUserService.queryByList(model);
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",model.getPager().getRowCount());
		jsonMap.put("rows", dataList);
		return jsonMap;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Object add(SysUsers user){
		user.setPassword(DigestUtils.md5DigestAsHex(user.getUser_name().getBytes()));
		sysUserService.add(user);
		return sendSuccessMessage("处理成功");
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public Object update(SysUsers user){
		sysUserService.update(user);
		return sendSuccessMessage("处理成功");
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(String id){
		sysUserService.delete(id);
		return sendSuccessMessage("删除成功");
	}
	
	@RequestMapping(value="/updateUserStatus")
	@ResponseBody
	public Object updateUserStatus(SysUsers users){
		sysUserService.updateUserStatus(users);
		return sendSuccessMessage("处理成功");
	}
	
	//登录
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public Object login(@RequestParam(value="userName")String userName,
						@RequestParam(value="passWord")String passWord){
		if(StringUtils.isEmpty(userName)){
			return sendFailureMessage("帐号不能为空");
		}
		if(StringUtils.isEmpty(passWord)){
			return sendFailureMessage("密码不能为空");
		}
		try{
			UsernamePasswordToken token = new UsernamePasswordToken(userName,DigestUtils.md5DigestAsHex(passWord.getBytes()).toCharArray());
			Subject user = SecurityUtils.getSubject();
			user.login(token);
		} catch (UnknownAccountException e) {
			return sendFailureMessage("账号不存在");
        } catch (DisabledAccountException e) {
        	return sendFailureMessage("账号未启用");
        } catch (IncorrectCredentialsException e) {
        	return sendFailureMessage("帐号或密码错误");
        } catch (Exception e) {
        	return sendFailureMessage("系统异常");
        }
		return sendSuccessMessage("登录成功");
	}
	
    /**
     * 退出
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/logout")
    @ResponseBody
    public Object logout(HttpServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return sendSuccessMessage("退出成功");
    }
}
